<?php
class Merchant_Model_ReceivingItem
{

	private $_table;
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_ReceivingItem();
	}


	public function add($row){
		return $this->_table->insert($row);
	}

	public  function update($row,$value,$field='ri_id'){
		$row['modified'] = date("Y-m-d H:i:s");
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?",$value);
		return $this->_table->update($row, $where);

	}


	public function delete($value,$field='ri_id'){

		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $value);

		return $this->_table->delete($where);

	}
	
	public function getByCond($cond,$type,$order='',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		if(isset($cond['status']) && $cond['status']!==''){
			$select->where('status = ?',$cond['status']);
		}
		if(isset($cond['product_id']) && $cond['product_id']!==''){
			$select->where('product_id = ?',$cond['product_id']);
		}
		if(isset($cond['receiving_code']) && $cond['receiving_code']!==''){
			$select->where('receiving_code = ?',$cond['receiving_code']);
		}
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		$select->order($order);
		$select->limit($count,$offset);
		return $this->_table->fetchAll($select)->toArray();
	}
	
	public function getAllByValue($value,$field){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchAll($select);
	}

	public function getByReceivingAsnPid($cond){
		$select = $this->_table->select()->where('product_id = ?', $cond['product_id'])
										->where("receiving_code=?",$cond['receiving_code']);
		return $this->_table->fetchRow($select);
	}
 
}